package com.cms.controller.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;
import org.apache.http.client.utils.DateUtils;

import com.cms.Feedback;
import com.cms.entity.Model;
import com.cms.entity.ModelField;
import com.cms.routes.RouteMapping;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
/**
 * Controller - 自定义表
 * 
 * 
 * 
 */
@RouteMapping(url = "/admin/table")
public class TableController extends BaseController {
    
    /**
     * 添加
     */
    public void add() {
        Long modelId = getParaToLong("modelId");
        List<ModelField> modelFields = new ModelField().dao().findList(modelId, null,getCurrentSite().getId());
        setAttr("modelFields",modelFields);
        setAttr("modelId", modelId);
        render(getView("table/add"));
    }
    
    
    /**
     * 保存
     */
    public void save() {
        Long modelId = getParaToLong("modelId");
        Model model = new Model().dao().findById(modelId);
        List<ModelField> modelFields = new ModelField().dao().findList(modelId, null,getCurrentSite().getId());
        StringBuilder column = new StringBuilder();
        StringBuilder valueSql = new StringBuilder();
        for(ModelField modelField : modelFields){
            String value = getPara(modelField.getName());
            column.append(",`"+modelField.getName()+"`");
            valueSql.append(",'"+value.replace("\r", "\\r")+"'");
        }
        String now = DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
        String insertSql = "INSERT INTO `"+model.getTableName()+"` (`createDate`,`modifyDate`,`siteId`"+column.toString()+")"+" values("+"'"+now+"',"+"'"+now+"',"+getCurrentSite().getId()+valueSql.toString()+");";
        Db.update(insertSql);
        redirect(getListQuery("/admin/table/data?modelId="+modelId));
    }
    
    
    /**
     * 编辑
     */
    public void edit() {
        Long modelId = getParaToLong("modelId");
        Long id = getParaToLong("id");
        Model model = new Model().dao().findById(modelId);
        Record record = Db.findById(model.getTableName(), id);
        List<ModelField> modelFields = new ModelField().dao().findList(modelId, null,getCurrentSite().getId());
        setAttr("modelFields",modelFields);
        setAttr("record", record);
        setAttr("modelId", modelId);
        render(getView("table/edit"));
    }

    /**
     * 更新
     */
    public void update() {
        Long id = getParaToLong("id");
        Long modelId = getParaToLong("modelId");
        Model model = new Model().dao().findById(modelId);
        List<ModelField> modelFields = new ModelField().dao().findList(modelId, null,getCurrentSite().getId());
        StringBuilder setSql = new StringBuilder();
        for(ModelField modelField : modelFields){
            String value = getPara(modelField.getName());
            setSql.append(",`"+modelField.getName()+"` = '"+value.replace("\r", "\\r")+"'");
        }
        String now = DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
        String updateSql = "UPDATE `"+model.getTableName()+"` SET `modifyDate` = "+"'"+now+"'"+setSql.toString()+" WHERE `id` = "+id+";";
        Db.update(updateSql);
        redirect(getListQuery("/admin/table/data?modelId="+modelId));
    }

   /**
     * 列表
     */
    public void list(){
        setAttr("models", new Model().dao().findList(Model.TABLE_TYPE,getCurrentSite().getId()));
        render(getView("table/list"));
    }
    
    /**
     * 列表
     */
    public void data() {
        Long modelId = getParaToLong("modelId");
        Integer pageNumber = getParaToInt("pageNumber");
        if(pageNumber==null){
            pageNumber = 1;
        }
        Model model = new Model().dao().findById(modelId);
        List<ModelField> modelFields = new ModelField().dao().findList(modelId, true,getCurrentSite().getId());
        setAttr("modelFields",modelFields);
        setAttr("page", Db.paginate(pageNumber, PAGE_SIZE, "select * ", "from "+model.getTableName()+" where siteId="+getCurrentSite().getId()));
        setAttr("modelId", modelId);
        render(getView("table/data"));
    }

    /**
     * 删除
     */
    public void delete() {
        Long modelId = getParaToLong("modelId");
        Model model = new Model().dao().findById(modelId);
        Long ids[] = getParaValuesToLong("ids");
        if(ArrayUtils.isNotEmpty(ids)){
            for(Long id:ids){
                Db.deleteById(model.getTableName(), id);
            }
        }
        renderJson(Feedback.success(new HashMap<>()));
    }

}
